package org.peterbjornx.formats;

import org.peterbjornx.io.*;
import org.peterbjornx.Client;

import java.io.IOException;
import java.awt.*;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: PeterbjornX
 * Date: Apr 7, 2009
 * Time: 7:22:17 PM
 * Defines a interface by JAGeX format
 */
public class RSInterface {
    private boolean type1bool;
    private short type1word;
	private static AbstractFileStore spritestore = null;
	private static AbstractFileStore interstore = null;
	private static AbstractJagexImageReader imgfetch = null;
    private static HashMap<String,Image> imagecache = new HashMap<String,Image>();

	public static void setFileStores(AbstractFileStore inter,AbstractFileStore sprite){
		interstore = inter;
		spritestore = sprite;
		imgfetch = new FileJagexImageReader(spritestore);
	}
	
    public static void unpack() throws IOException
    {
		RSInputStream stream = interstore.getFile("DATA");
           int parentID = -1;
           int interfacecount = stream.readUShort();
           int rid = 1;
           interfaceCache = new RSInterface[interfacecount+1];
           while(rid < interfacecount)
           {
               int id = stream.readUShort();
               if (id == -1) id = 65535;
               if(id == 65535)
               {
                   parentID = stream.readUShort();
                   id = stream.readUShort();

               }
               rid++;
               RSInterface inter = interfaceCache[id] = new RSInterface();
               //float pxc = ((float)rid) / ((float)interfacecount);
               //pxc*=100;
               //Client.instance.setloadingtext("Loading interfaces - "+(int)pxc+"%",30);
               inter.id = id;
               inter.parentID = parentID;
               inter.type = stream.readByte()&0xff;
               inter.atActionType = stream.readByte()&0xff;
               inter.contentType = stream.readUShort();
               inter.width = stream.readUShort();
               inter.height = stream.readUShort();
               inter.shadeColor = stream.readByte()&0xff;
               inter.mOverInterToTrigger = stream.readByte()&0xff;
               if(inter.mOverInterToTrigger != 0)  //If not zero , readword
                   inter.mOverInterToTrigger = (inter.mOverInterToTrigger - 1 << 8) + stream.readByte()&0xff;
               else
                   inter.mOverInterToTrigger = -1;
               int i1 = stream.readByte()&0xff;
               if(i1 > 0)
               {
                   inter.valueCompareType = new int[i1];
                   inter.requiredValues = new int[i1];
                   for(int j1 = 0; j1 < i1; j1++)
                   {
                       inter.valueCompareType[j1] = stream.readByte()&0xff;
                       inter.requiredValues[j1] = stream.readUShort();
                   }

               }
               int k1 = stream.readByte()&0xff;
               if(k1 > 0)
               {
                   inter.cs1Opcodes = new int[k1][];
                   for(int l1 = 0; l1 < k1; l1++)
                   {
                       int i3 = stream.readUShort();
                       inter.cs1Opcodes[l1] = new int[i3];
                       for(int l4 = 0; l4 < i3; l4++)
                           inter.cs1Opcodes[l1][l4] = stream.readUShort();

                   }

               }
               if(inter.type == 0)
               {
                   inter.scrollMax = stream.readUShort();
                   inter.isMouseoverTriggered = stream.readByte() == 1;
                   int childCount = stream.readUShort();
                   inter.children = new int[childCount];
                   inter.childX = new int[childCount];
                   inter.childY = new int[childCount];
                   for(int j3 = 0; j3 < childCount; j3++)
                   {
                       inter.children[j3] = stream.readUShort();
                       inter.childX[j3] = stream.readSignedWord();
                       inter.childY[j3] = stream.readSignedWord();
                   }

               }
               if(inter.type == 1)
               {
                   inter.type1word = stream.readUShort();
                   inter.type1bool = stream.readByte() == 1;
               }
               if(inter.type == 2)
               {
                   inter.inv = new int[inter.width * inter.height];
                   inter.invStackSizes = new int[inter.width * inter.height];
                   inter.invItemsCanBeSwapped = stream.readByte() == 1;
                   inter.isInventoryInterface = stream.readByte() == 1;
                   inter.usableItemInterface = stream.readByte() == 1;
                   inter.invSwapDeletesTargetSlot = stream.readByte() == 1;
                   inter.invSpritePadX = stream.readByte()&0xff;
                   inter.invSpritePadY = stream.readByte()&0xff;
                   inter.spritesX = new int[20];
                   inter.spritesY = new int[20];
                   inter.images = new Image[20];
                   for(int j2 = 0; j2 < 20; j2++)
                   {
                       int k3 = stream.readByte()&0xff;
                       if(k3 == 1)
                       {
                           inter.spritesX[j2] = stream.readSignedWord();
                           inter.spritesY[j2] = stream.readSignedWord();
                           String s1 = stream.readString10T();
                           if(s1.length() > 0)
                           {
                               int i5 = s1.lastIndexOf(",");
                               inter.images[j2] = inter.getSprite(Integer.parseInt(s1.substring(i5 + 1)), s1.substring(0, i5));
                           }
                       }
                   }

                   inter.actions = new String[5];
                   for(int l3 = 0; l3 < 5; l3++)
                   {
                       inter.actions[l3] = stream.readString10T();
                       if(inter.actions[l3].length() == 0)
                           inter.actions[l3] = null;
                   }

               }
               if(inter.type == 3)
                   inter.filled = stream.readByte() == 1;
               if(inter.type == 4 || inter.type == 1)
               {
                   inter.midText = stream.readByte() == 1;
                   inter.font = stream.readByte()&0xff;
                   inter.shaded = stream.readByte() == 1;
               }
               if(inter.type == 4)
               {
                   inter.disabledText = stream.readString10T();
                   inter.enabledText = stream.readString10T();
               }
               if(inter.type == 1 || inter.type == 3 || inter.type == 4)
                   inter.disabledColor = stream.readInt();
               if(inter.type == 3 || inter.type == 4)
               {
                   inter.enabledColor = stream.readInt();
                   inter.disabledSecondColor = stream.readInt();
                   inter.enabledSecondColor = stream.readInt();
               }
               if(inter.type == 5)
               {
                   String s = stream.readString10T();
                   if(s.length() > 0)
                   {
                       int i4 = s.lastIndexOf(",");
                       inter.disabledImage = inter.getSprite(Integer.parseInt(s.substring(i4 + 1)),  s.substring(0, i4));
                   }
                   s = stream.readString10T();
                   if(s.length() > 0)
                   {
                       int j4 = s.lastIndexOf(",");
                       inter.enabledImage = inter.getSprite(Integer.parseInt(s.substring(j4 + 1)),  s.substring(0, j4));
                   }
               }
               if(inter.type == 6)
               {
                   int l = stream.readByte()&0xff;
                   if(l != 0)
                   {
                       inter.mediaType = 1;
                       inter.mediaID = (l - 1 << 8) + stream.readByte()&0xff;
                   }
                   l = stream.readByte()&0xff;
                   if(l != 0)
                   {
                       inter.mediaTypeEnab = 1;
                       inter.mediaIdEnab = (l - 1 << 8) + stream.readByte()&0xff;
                   }
                   l = stream.readByte()&0xff;
                   if(l != 0)
                       inter.disabledAnimation = (l - 1 << 8) + stream.readByte()&0xff;
                   else
                       inter.disabledAnimation = -1;
                   l = stream.readByte()&0xff;
                   if(l != 0)
                       inter.enabledAnimation = (l - 1 << 8) + stream.readByte()&0xff;
                   else
                       inter.enabledAnimation = -1;
                   inter.modelZoom = stream.readUShort();
                   inter.modelRotation = stream.readUShort();
                   inter.modelRotation2 = stream.readUShort();
               }
               if(inter.type == 7)
               {
                   inter.inv = new int[inter.width * inter.height];
                   inter.invStackSizes = new int[inter.width * inter.height];
                   inter.midText = stream.readByte() == 1;
                   inter.font = stream.readByte()&0xff;
                   inter.shaded = stream.readByte() == 1;
                   inter.disabledColor = stream.readInt();
                   inter.invSpritePadX = stream.readSignedWord();
                   inter.invSpritePadY = stream.readSignedWord();
                   inter.isInventoryInterface = stream.readByte() == 1;
                   inter.actions = new String[5];
                   for(int k4 = 0; k4 < 5; k4++)
                   {
                       inter.actions[k4] = stream.readString10T();
                       if(inter.actions[k4].length() == 0)
                           inter.actions[k4] = null;
                   }

               }
               if(inter.atActionType == 2 || inter.type == 2)
               {
                   inter.selectedActionName = stream.readString10T();
                   inter.spellName = stream.readString10T();
                   inter.spellUsableOn = stream.readUShort();
               }

               if(inter.type == 8)
                 inter.disabledText = stream.readString10T();
               if(inter.atActionType == 1 || inter.atActionType == 4 || inter.atActionType == 5 || inter.atActionType == 6)
               {
                   inter.tooltip = stream.readString10T();
                   if(inter.tooltip.length() == 0)
                   {
                       if(inter.atActionType == 1)
                           inter.tooltip = "Ok";
                       if(inter.atActionType == 4)
                           inter.tooltip = "Select";
                       if(inter.atActionType == 5)
                           inter.tooltip = "Select";
                       if(inter.atActionType == 6)
                           inter.tooltip = "Continue";
                   }
               }
           }
       }

    private Image getSprite(int i, String s) throws IOException {
        Image ii = imagecache.get(s+"-"+i);
        if (ii != null)
            return ii;
        ii = imgfetch.getImage(s,i);
        imagecache.put(s+"-"+i,ii);
        return ii;
    }

    public Image disabledImage;
    public int anInt208;
    public Image images[];
    public static RSInterface interfaceCache[];
    public int requiredValues[];
    public int contentType;
    public int spritesX[];
    public int disabledSecondColor;
    public int atActionType;
    public String spellName;
    public int enabledColor;
    public int width;
    public String tooltip;
    public String selectedActionName;
    public boolean midText;
    public int scrollPosition;
    public String actions[];
    public int cs1Opcodes[][];
    public boolean filled;
    public String enabledText;
    public int mOverInterToTrigger;
    public int invSpritePadX;
    public int disabledColor;
    public int mediaType;
    public int mediaID;
    public boolean invSwapDeletesTargetSlot;
    public int parentID;
    public int spellUsableOn;
    public int enabledSecondColor;
    public int children[];
    public int childX[];
    public boolean usableItemInterface;
    public int font;
    public int invSpritePadY;
    public int valueCompareType[];
    public int animationFrame;
    public int spritesY[];
    public String disabledText;
    public boolean isInventoryInterface;
    public int id;
    public int invStackSizes[];
    public int inv[];
    public int shadeColor;
    private int mediaTypeEnab;
    private int mediaIdEnab;
    public int disabledAnimation;
    public int enabledAnimation;
    public boolean invItemsCanBeSwapped;
    public Image enabledImage;
    public int scrollMax;
    public int type;
    public int xDrawOffset;
    public int yDrawOffset;
    public boolean isMouseoverTriggered;
    public int height;
    public boolean shaded;
    public int modelZoom;
    public int modelRotation;
    public int modelRotation2;
    public int childY[];

}
